//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.keafmd.result;

import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RespResult<V> {

    @Override
    public String toString() {
        return "RespResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    private int code;
    private String message;
    private V data;

    public RespResult() {
        this(200, (String)null, (V) null);
    }

    private RespResult(int code, String message, V data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <V> RespResult<V> failed(int code) {
        return new RespResult(code, ErrEums.get(code).getMessage(), (Object)null);
    }

    public static <V> RespResult<V> failed(int code, String message) {
        return new RespResult(code, message, (Object)null);
    }

    public static <V> RespResult<V> failed(IErrEums err) {
        return new RespResult(err.getCode(), err.getMessage(), (Object)null);
    }

    public static <V> RespResult<V> failed(String message) {
        return new RespResult(ErrEums.ERROR.getCode(), strOf(message, ErrEums.ERROR.getMessage()), (Object)null);
    }

    public static <V> RespResult<V> successful() {
        return new RespResult(200, "成功", (Object)null);
    }

    public static <V> RespResult<V> success(V data) {
        return new RespResult(200, "成功", data);
    }

    public boolean success() {
        return this.code == 200;
    }

    public boolean fail() {
        return this.code != 200;
    }

    public int getCode() {
        return this.code;
    }

    public RespResult<V> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return this.message;
    }

    public RespResult<V> setMessage(String message) {
        this.message = message;
        return this;
    }

    public V getData() {
        return this.data;
    }

    public RespResult<V> setData(V data) {
        this.data = data;
        return this;
    }

    public static String strOf(Object str, String defValue) {
        if (str == null) {
            return defValue;
        } else {
            String v;
            if (str instanceof Date) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                v = sdf.format((Date)str);
            } else {
                v = str.toString();
            }

            return "".equals(v.trim()) ? defValue : v;
        }
    }
}
